package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.Comment;
import com.blog.pojo.NodeKind;
import com.blog.pojo.ReturnMsg;
import com.blog.service.NodeService;
import com.blog.utils.FindRequestCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.TabExpander;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.RecursiveTask;

/**
 * @Description:T0D0
 * @Author XieLiKui
 * @Date 2021/10/6
 **/
@RestController
public class NodeController {

    @Resource(name = "nodeService")
    private NodeService nodeService;

    @Autowired
    private FindRequestCacheUtil cacheUtil;

    @RequestMapping("/findNodeKind")
    public ArrayList<NodeKind> findNodeKind() {
        return nodeService.findNodeKind();
    }

    @RequestMapping("/findBlog")
    public HashMap<String, Object> findBlog(String kindName, String sort ,int reqPage) {
        return nodeService.findBlog(kindName,sort,reqPage);
    }

    //通过关键字模糊查询文章
    @RequestMapping("/findBlogByKeywords")
    public HashMap<String, Object> findBlogByKeyword(String keywords , String sort , int reqPage) {
        HashMap<String, Object> result = nodeService.findBlogByKeyword(keywords , sort , reqPage);
        return result;
    }

    //通过id查询文章详情
    @RequestMapping("/findBlogById")
    public Article findBlogById(HttpServletRequest request , int id) {
        boolean cacheFlag = cacheUtil.findCache(1, id, request.getRemoteAddr());
        if(cacheFlag){
            nodeService.updateArticleView(id);
        }
        return nodeService.findBlogById(id);
    }

    //查询评论
    @RequestMapping("/findBlogCommentById")
    public List<Comment> findBlogCommentById(int id) {
        return nodeService.findBlogCommentById(id);
    }

    //文章点赞
    @RequestMapping("/updateArticleApprove")
    public ReturnMsg updateArticleApprove(int id , HttpServletRequest request) {
        ReturnMsg returnMsg = new ReturnMsg();
        boolean cacheFlag = cacheUtil.findCache(2, id, request.getRemoteAddr());
        if(!cacheFlag){
            returnMsg.setMsg("已经点赞过了哦");
        }else {
            boolean updateFlag = nodeService.updateArticleApprove(id);
            if(updateFlag){
                returnMsg.setFlag(true);
            }else {
                returnMsg.setFlag(false);
                returnMsg.setMsg("系统错误，点赞失败");
            }
        }
        return returnMsg;
    }

    //评论点赞
    @RequestMapping("/updateCommentApprove")
    public ReturnMsg updateCommentApprove(int id, HttpServletRequest request) {
        ReturnMsg returnMsg = new ReturnMsg();
        boolean cacheFlag = cacheUtil.findCache(3, id, request.getRemoteAddr());
        if(!cacheFlag){
            returnMsg.setMsg("已经点赞过了哦");
        }else {
            boolean updateFlag = nodeService.updateCommentApprove(id);
            if(updateFlag){
                returnMsg.setFlag(true);
            }else {
                returnMsg.setFlag(false);
                returnMsg.setMsg("系统错误，点赞失败");
            }
        }
        return returnMsg;
    }

    //添加评论
    @RequestMapping("/insertArticleComment")
    public boolean insertArticleComment(HttpServletRequest request , int aid, String content) {
        return nodeService.insertArticleComment(aid,content);
    }

    //按ID查询文章类型
    @RequestMapping("/findKindByArticleId")
    public List<String> findKindByArticleId(int id) {
        return nodeService.findKindByArticleId(id);
    }

    //添加文章
    @RequestMapping("/addArticle")
    public void addArticle(String title, String[] kinds, String content, MultipartFile uploadImg) throws IOException {

        Article article = new Article();

        if(uploadImg==null){
            article.setSrc("static/images/article_preview/default_preview/"+kinds[0]+".jpg");
        }else {
            //设置文件保存路径
            String filePath = "D:/Program Date/Git/gitProject/xlkblog_2.0/xlkblog_web_2.0/static/images/article_preview/";
            File file = new File(filePath);
            if (!file.exists()){
                file.mkdir();
            }
            //设置文件名
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileName = uploadImg.getOriginalFilename();
            fileName = uuid+fileName;
            //保存
            uploadImg.transferTo(new File(filePath,fileName));
            article.setSrc("../static/images/article_preview/"+fileName);
        }

        Date date = new Date();
        article.setTitle(title);
        article.setTime(date);
        article.setDetail(content);

        nodeService.addArticle(article,kinds);
    }

    //图片上传
    @RequestMapping("/uploadImg")
    public HashMap<String,Object> uploadImg(MultipartFile uploadImg, HttpServletRequest request) throws IOException {
        //设置文件保存路径
        String filePath = "D:/Program Date/Git/gitProject/xlkblog_2.0/xlkblog_web_2.0/static/images/article_detail/";
        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
        //设置文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uploadImg.getOriginalFilename().replaceAll(" ", "");
        fileName = uuid+fileName;
        //保存
        uploadImg.transferTo(new File(filePath,fileName));

        HashMap<String,String> imgDate = new HashMap<>();
        imgDate.put("url","../static/images/article_detail/"+fileName);
        imgDate.put("alt","图片");
        imgDate.put("href","#");
        ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
        arrayList.add(imgDate);

        HashMap<String,Object> returnMsg = new HashMap<>();
        returnMsg.put("errno", 0);
        returnMsg.put("data",arrayList);

        return returnMsg;
    }
}
